/*****************************************************/
//Test Display main 

#include <stdio.h>

#include <csl.h>
#include <csl_cache.h>
#include <csl_edma.h>
#include <csl_timer.h>
#include <csl_irq.h>
#include <csl_emif.h>
#include <csl_emifa.h>

#include <csl_dat.h>
#include "vportdis.h"

VP_Handle vpHchannel0;
VP_Handle vpHchannel1;
VP_Handle vpHchannel2;


int numPixels = 720;
int numLines  = 576;

Uint32 capYbuffer  = 0x80000000;
Uint32 capCbbuffer = 0x800675c0; 
Uint32 capCrbuffer = 0x8009b0a0;

Uint32 disYbuffer  = 0x80100000;
Uint32 disCbbuffer = 0x801675c0; 
Uint32 disCrbuffer = 0x8019b0a0;

int portNumber;
Uint32 fillValue[2] = {0x12341234,0x12341234};
Uint32 transferId;

extern far void vectors();
extern volatile Uint32 disNewFrame;
extern int global_counter;

/* Spin in a delay loop for delay iterations */
void board_wait(Uint32 delay)
{
    volatile Uint32 i, n;

    n = 0;
    for (i = 0; i < delay; i++)
    {
        n = n + 1;
    }
}

void test_diplay_main()
{
	int i;

	//Point to the IRQ vector table
    IRQ_setVecs(vectors);
    IRQ_nmiEnable();
    IRQ_globalEnable();
    IRQ_map(IRQ_EVT_VINT2, 11);
    IRQ_reset(IRQ_EVT_VINT2);

    DAT_open(DAT_CHAANY, DAT_PRI_LOW, DAT_OPEN_2D);	

	portNumber = 2;
	vpHchannel2 = bt656_8bit_ncfd(portNumber);

	for(i=0;i<numLines;i++)
	{
		transferId = DAT_fill((void *)(disYbuffer + i * numPixels), numPixels,(Uint32*)&fillValue);
		while (DAT_busy(transferId));
		transferId = DAT_fill((void *)(disCbbuffer + i * (numPixels >> 1)), numPixels>>1, (Uint32*)&fillValue);
		while (DAT_busy(transferId));
		transferId = DAT_fill((void *)(disCrbuffer + i * (numPixels >> 1)), numPixels>>1, (Uint32*)&fillValue);
		while (DAT_busy(transferId));
	 }

	bt656_display_start(vpHchannel2);
	for(;;)
	{
		if(disNewFrame == 1)
		{
			disNewFrame =0;
			for(i=0;i<numLines;i++)
			{
				transferId = DAT_fill((void *)(disYbuffer + i * numPixels), numPixels,(Uint32*)&fillValue);
				while (DAT_busy(transferId));
				transferId = DAT_fill((void *)(disCbbuffer + i * (numPixels >> 1)), numPixels>>1, (Uint32*)&fillValue);
				while (DAT_busy(transferId));
				transferId = DAT_fill((void *)(disCrbuffer + i * (numPixels >> 1)), numPixels>>1, (Uint32*)&fillValue);
				while (DAT_busy(transferId));
			 }
		}
		board_wait(5000000);
//		printf("\n-------------------------------------------------");
//		printf("\nDot global_counter:\t%d\n",global_counter);
	}
}
//End here
